﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using Lib.Helper;
using MSIACCSL2.CoaServices;
using MSIACCSL2.Command;
using MSIACCSL2.SupplierServices;
using MSIACCSL2.Views.Supplier;
using WcfService1.DTO;

namespace MSIACCSL2.ViewModels
{
    public class SupplierSearchViewModel : ViewModelBase
    {
        #region Delegates

        public delegate void Completed(Object param);

        #endregion

        private static string OrderType;
        private static string PropertySort;
        public static Mode mode;
        private static SupplierDTO _Supplier;
        private static Add frmAdd;
        public static Completed OnSearchCompleted;
        private readonly CoaServicesClient CoaClient;
        private readonly SupplierServicesClient client;
        private readonly ValidationHandler validationHandler;
        private int CurrentPage;
        private ObservableCollection<int> Pages;
        private ObservableCollection<CoaDTO> _COAs;
        private bool _IsBusy;
        private PagedCollectionView _Suppliers;
        private List<string> _errors;
        private Visibility _messageVisibility;
        private PagedCollectionView _pageSources;
        private ObservableCollection<SupplierDTO> _parents;
        private Search frmSearch;


        public SupplierSearchViewModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                client = new SupplierServicesClient();
                CoaClient = new CoaServicesClient();
                SearchSupplierCommand = new DelegateCommand(SearchSupplierByName, CanSearch);
                validationHandler = new ValidationHandler();
                MessageVisibility = Visibility.Collapsed;
            }
        }

        public PagedCollectionView Suppliers
        {
            get { return _Suppliers; }

            private set
            {
                if (Suppliers == value) return;
                _Suppliers = value;
                int i = (CurrentPage*30);
                foreach (object Supplier in Suppliers)
                {
                    i += 1;

                    ((SupplierDTO) Supplier).No = i;
                }
                FirePropertyChanged("Suppliers");
            }
        }


        public bool IsBusy
        {
            get { return _IsBusy; }
            internal set
            {
                _IsBusy = value;
                FirePropertyChanged("IsBusy");
            }
        }


        //Buat binding ke child form waktu mau edit
        public static SupplierDTO Supplier
        {
            get { return _Supplier; }
            private set
            {
                if (Supplier == value) return;
                _Supplier = value;
                FirePropertyChanged2("Supplier");
            }
        }


        public ICommand OnPageChanged { get; set; }
        public ICommand Save { get; set; }
        public ICommand New { get; set; }
        public ICommand Edit { get; set; }
        public ICommand Unload { get; set; }
        public ICommand SearchCommand { get; set; }
        public ICommand SearchResultCommand { get; set; }
        public ICommand SearchSupplierCommand { get; set; }

        public List<string> Errors
        {
            get { return _errors; }
            private set
            {
                if (Errors == value) return;
                _errors = value;
                FirePropertyChanged("Errors");
            }
        }

        public Visibility MessageVisibility
        {
            get { return _messageVisibility; }
            private set
            {
                if (MessageVisibility == value) return;
                _messageVisibility = value;
                FirePropertyChanged("MessageVisibility");
            }
        }


        public bool CanSearch(Object param)
        {
            return true;
        }

        public bool CanSearchResult(Object param)
        {
            return true;
        }


        public void Release(Object param)
        {
            param = null;
            Supplier = null;
            OrderType = null;
            PropertySort = null;
            mode = Mode.Idle;
        }


        public void SearchSupplierByName(Object param)
        {
            if (param == null) return;
            client.GetSupplierListCompleted += client_GetSupplierListCompleted;
            client.GetSupplierListAsync(param.ToString());
        }

        private void client_GetSupplierListCompleted(object sender, GetSupplierListCompletedEventArgs e)
        {
            client.GetSupplierListCompleted -= client_GetSupplierListCompleted;
            Suppliers = new PagedCollectionView(e.Result);
            if (Suppliers.Count < 1)
                MessageBox.Show("Supplier tidak ditemukan!");
        }
    }
}